export function setUserInfo(key,value){
    localStorage.setItem(`user-${key}`,JSON.stringify(value||[]))
}
export function getUserInfo(key){
    return  JSON.parse(localStorage.getItem(`user-${key}`)) 
}

export function removeUser(key){
    localStorage.removeItem(`user-${key}`)
}

export function lastLoginUser(){
    let userInfo = Object.entries(localStorage).filter(item=>item[0].startsWith('user-'))
    if(userInfo.length!==0){
        let lastUser = userInfo.filter(item=>item[1].includes(`"lastLogin":true`))
        if(lastUser.length!==0){
           let username= lastUser [0][0].replace('user-','')
            let lastUserInfo = getUserInfo(username)
            lastUserInfo.name=username
            return lastUserInfo
        }else{
            return
        }  
    }
    
}

export function resetLogin(){
    let lastUserInfo = lastLoginUser()
        lastUserInfo.lastLogin=false
    setUserInfo(lastUserInfo.name,lastUserInfo)  
}